#!/bin/bash

set -e
BASE_PATH=$(dirname $(readlink -f $0))
BUILD_PATH="$BASE_PATH/build"
OUT_PATH="$BASE_PATH/out"

# 检查构建环境
function check_build_environment() {
    # 检查系统版本
    if [ -f /etc/os-release ]; then
        . /etc/os-release
        if [ "$ID" != "ubuntu" ] || [ "$VERSION_ID" != "24.04" ]; then
            echo "错误: 此脚本只能在 Ubuntu 24.04 上运行"
            echo "当前系统: $PRETTY_NAME"
            exit 1
        fi
    else
        echo "错误: 无法确定操作系统版本"
        exit 1
    fi
    
    # 检查toolchains及其子目录
    if [ ! -d "$BASE_PATH/toolchains" ] || [ ! -d "$BASE_PATH/toolchains/$1" ]; then
        echo "错误: toolchains或其必需的子目录未下载"
        echo "请运行以下命令初始化并更新子模块:"
        echo "git submodule update --init --recursive"
        exit 1
    fi
    
    # 根据平台检查特定编译器
    if [ "$1" = "3516cv610" ]; then
        if [ ! -d "/opt/linux/x86-arm/arm-v01c02-linux-musleabi-gcc" ]; then
            echo "错误: 未找到海思编译器"
            echo "请确保已安装arm-v01c02-linux-musleabi-gcc并将其放置在/opt/linux/x86-arm/目录下"
            exit 1
        fi
    fi
}

export npm_config_registry=https://repo.huaweicloud.com/repository/npm/
export npm_config_strict_ssl=false

function usage() {
    echo "usage: ./build.sh [command] [platform] [mode]"
    echo "example: ./build.sh build m64x86 debug"
    echo "commands:"
    echo "  build           - Build the project (without web frontend)"
    echo "  web            - Build web frontend only"
    echo "  test           - Build and run tests"
    echo "  copy           - Copy files to QEMU image"
    echo "  run            - Run QEMU emulator"
    echo "  monitor        - Enter QEMU monitor mode"
    echo "  plug           - Plug USB device into QEMU"
    echo "  unplug         - Unplug USB device from QEMU"
    echo "  quit           - Quit QEMU emulator"
    echo ""
    echo "platform: m64x86, aarch64, 3516cv610 (default: aarch64)"
    echo "mode: debug, release, test (default: release)"
}

function build_platform() {
    PRODUCT_NAME=$1
    PLATFORM=$2
    MODE=${3:-debug}
    TEST=${4:-OFF}
    BUILD_WEB=${5:-ON}
    rm -rf "$BUILD_PATH/$PLATFORM"
    rm -rf "$OUT_PATH"
    mkdir -pv "$BUILD_PATH/$PLATFORM"
    cd "$BUILD_PATH"/"$PLATFORM"
    cmake $BASE_PATH -B$BUILD_PATH/$PLATFORM -DPRODUCT_NAME=$PRODUCT_NAME -DPLATFORM=$PLATFORM -DCMAKE_BUILD_TYPE=$MODE -DBUILD_TESTS=$TEST -DBUILD_WEB=$BUILD_WEB -G Ninja
    ninja
    cd "$BASE_PATH"
}

function build_package() {
    local platform=$1
    
    if [ -z "$platform" ]; then
        echo "Error: Platform not specified"
        echo "Usage: build_package <platform>"
        echo "  platform: 3516cv610/aarch64"
        exit 1
    fi
    
    case $platform in
        "3516cv610")
            source device/board/3516cv610/build_package.sh
        ;;
        "aarch64")
            source device/board/aarch64/build_package.sh
        ;;
        *)
            echo "Error: Package script not found for platform: $platform"
            exit 1
        ;;
    esac
}

function main() {
    PRODUCT_NAME=${1:-aarch64} # 第一个参数作为产品名称，默认为aarch64
    COMMAND=${2:-build}        # 第二个参数作为命令
    MODE=${3:-release}         # 第三个参数作为模式参数
    source device/qemu/qemu.sh
    
    # 根据产品名称设置平台
    case $PRODUCT_NAME in
        pa)
            PLATFORM="3516cv610"
        ;;
        pb)
            PLATFORM="aarch64"
        ;;
        *)
            echo "Unknown product: $PRODUCT_NAME"
            exit 1
        ;;
    esac
    
    # 检查构建环境
    check_build_environment "$PLATFORM"
    
    echo "Building product: $PRODUCT_NAME on platform: $PLATFORM"
    
    case $COMMAND in
        b | build)
            build_platform "$PRODUCT_NAME" "$PLATFORM" "$MODE" "OFF" "OFF"
        ;;
        t | test)
            build_platform "$PRODUCT_NAME" "$PLATFORM" "$MODE" "ON" "OFF"
        ;;
        p | package)
            build_platform "$PRODUCT_NAME" "$PLATFORM" "$MODE" "OFF" "ON"
            build_package "$PLATFORM"
        ;;
        a | all)
            build_platform "$PRODUCT_NAME" "$PLATFORM" "$MODE" "OFF" "ON"
        ;;
        cp | copy)
            qemu_copy "$PLATFORM"
        ;;
        run)
            qemu_run "$PLATFORM"
        ;;
        monitor)
            qemu_monitor
        ;;
        plug)
            qemu_plug_usb
        ;;
        unplug)
            qemu_unplug_usb
        ;;
        quit)
            qemu_quit
        ;;
        *)
            usage
            exit 1
        ;;
    esac
}

main "$@"
